package com.shjn.squirrel.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.shjn.squirrel.R;
import com.shjn.squirrel.application.MyApplication;
import com.shjn.squirrel.bean.storageBean.WorkSetting;
import com.shjn.squirrel.model.LocalStorage;
import com.shjn.squirrel.utils.SoundPlayUtils;

/**
 * 顶部头标(显示设备ID、客服电话、我是回收员入口)
 */
public class TopFragment extends BaseFragment implements View.OnClickListener {

    private static TopFragment instance;
    private TextView mDeviceNum;
    private TextView mServiceTel;
    public Button mBtnMyRecycler;
    private boolean mIsVisble = true;

    public TopFragment() {
    }

    public synchronized static TopFragment getInstance() {
        if (instance == null) {
            instance = new TopFragment();
        }
        return instance;
    }

    /**
     * 设置设备ID
     *
     * @param deviceID
     */
    public void setDeviceID(String deviceID) {
        if (this.isAdded() && mDeviceNum != null) {
            mDeviceNum.setText("机器编号: " + deviceID);
        }
    }

    /**
     * 设置客服电话
     *
     * @param serviceTel
     */
    public void setServiceTel(String serviceTel) {
        if (this.isAdded() && mServiceTel != null) {
            mServiceTel.setText("客服电话: " + serviceTel);
        }
    }

    /**
     * 设置回收员入口显示或隐藏
     *
     * @param visible
     * @return
     */
    public TopFragment setRecyclerPortalState(boolean visible) {
        mIsVisble = visible;
        return this;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return createView(inflater, R.layout.top, container);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    /**
     * 初始化相关控件
     */
    private void initView() {
        mDeviceNum = (TextView) mView.findViewById(R.id.tv_device_num);
        mServiceTel = (TextView) mView.findViewById(R.id.tv_service_tel);
        mBtnMyRecycler = (Button) mView.findViewById(R.id.btn_my_recycler);
//        String deviceID = LocalStorage.getDeviceID(MyApplication.getInstance());
//        WorkSetting workSetting = LocalStorage.getWorkSetting(MyApplication.getInstance());
        //----------测试用-------------//
        mDeviceNum.setText("机器编号: 201900032");
        mServiceTel.setText("客服电话: 021-12345678");
//        if (!deviceID.isEmpty()) {
//            mDeviceNum.setText("机器编号: " + deviceID);
//        } else {
//            mDeviceNum.setText("机器编号: — — — — —");
//        }
//        if (workSetting.getServiceTel() != null && !workSetting.getServiceTel().isEmpty()) {
//            mServiceTel.setText("客服电话: " + workSetting.getServiceTel());
//        } else {
//            mServiceTel.setText("客服电话: — — — — —");
//        }
        if (mIsVisble) {
            mBtnMyRecycler.setVisibility(View.VISIBLE);
        } else {
            mBtnMyRecycler.setVisibility(View.INVISIBLE);
        }
        mBtnMyRecycler.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //跳转到回收员扫码登录页面
            case R.id.btn_my_recycler:
                if (!RecyclerScanFragment.getInstance().isAdded()) {
                    SoundPlayUtils.play(11);
                    mBtnMyRecycler.setVisibility(View.INVISIBLE);
                    mActivity.getFragmentManager().beginTransaction()
                            .replace(R.id.mid, RecyclerScanFragment.getInstance())
                            .commit();
                }
                break;
            default:
                break;
        }
    }
}
